#delimit;

set more off;

capture log close;

local t1="specify here the folder path where the data are stored";
local t2="specify here the folder path where you want the output data to be stored";

log using `t2'mergewave1_rel231_ext, replace;

drop _all;

set mem 550m;



use `t1'sharew1_rel2-3-1_ch1;
sort mergeid;

merge mergeid using `t1'sharew1_rel2-3-1_dn1;
tab _merge; *Here merge is not 3 for all cases since we dropped Israel;
drop _merge;
sort mergeid;

merge mergeid using `t1'sharew1_rel2-3-1_ep1;
tab _merge;
drop _merge;
sort mergeid;
merge mergeid using `t1'sharew1_rel2-3-1_ph1;
tab _merge;
drop _merge;
sort mergeid;
merge mergeid using `t1'sharew1_rel2-3-1_sp1;
tab _merge;
drop _merge;
sort mergeid;
merge mergeid using `t1'sharew1_rel2-3-1_hc1;
tab _merge;
drop _merge;
sort mergeid;

merge mergeid using `t1'sharew1_rel2-3-1_gv_health1;
tab _merge;
drop _merge;
sort mergeid;
merge mergeid using `t1'sharew1_rel2-3-1_gv_isced1;
tab _merge;
drop if _merge!=3; *Elimino a los de Israel;
drop _merge;


sort hhid cvid;
merge hhid cvid using `t1'sharew1_rel2-3-1_cv_r1;
tab _merge;

keep if _merge==3;*We drop Israel and non-eligibles or non-interviewed people;
sort mergeid;
drop _merge;
merge mergeid using `t1'sharew1_rel2-3-1_imputations1;
tab _merge;
keep if implicat==1;
keep if _merge==3;*We drop Israel and non-eligibles or non-interviewed people;
drop _merge;
sort mergeid;


*We recover info on gender, age and health of parents who are interviewed;
gen relrpers1=relrpers if relrpers==5 | relrpers==6;
replace relrpers1=0 if (relrpers==5 | relrpers==6) & hhid==hhid[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen relrpers1b=relrpers if relrpers1==0 & (relrpers==5 | relrpers==6);
egen relrpers2=sum(relrpers1), by (hhid);
sort mergeid;
egen relrpers2b=sum(relrpers1b), by (hhid);
sort mergeid;


gen pgender=gender if (relrpers==5 | relrpers==6);  
replace pgender=0 if (relrpers==5 | relrpers==6) & hhid==hhid[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen pgender2=gender if pgender==0 & (relrpers==5 | relrpers==6);
egen ppgender=sum(pgender), by (hhid);
sort mergeid;
egen ppgender2=sum(pgender2), by (hhid);
sort mergeid;

gen age=int_year-dn003;
gen page=age if (relrpers==5 | relrpers==6);  
replace page=0 if (relrpers==5 | relrpers==6) & hhid==hhid[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen page2=age if page==0 & (relrpers==5 | relrpers==6);
egen ppage=sum(page), by (hhid);
sort mergeid;
egen ppage2=sum(page2), by (hhid);
sort mergeid;

gen phealth=spheu if (relrpers==5 | relrpers==6);
replace phealth=0 if (relrpers==5 | relrpers==6) & hhid==hhid[_n-1] & (relrpers[_n-1]==5 | relrpers[_n-1]==6)  ;
gen phealth2=spheu if phealth==0 & (relrpers==5 | relrpers==6);
egen pphealth=sum(phealth), by (hhid);
sort mergeid;
egen pphealth2=sum(phealth2), by (hhid);
sort mergeid;

sort mergeid;

tab country;
save `t2'mergewave1_rel231_ext.dta, replace;



log close;
